package com.controller;

import com.bean.UserBean;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

@SessionAttributes(types = {UserBean.class})
@Controller
@RequestMapping("/user")
public class UserController {

    @GetMapping("/toReg")
    public String toReg() {
        return "regist.jsp";
    }

    @RequestMapping("/register")
    public String doRegister(@RequestParam("u1") String uname, @RequestParam("p1") String pwd,
                             Model model) {
        System.out.println("获取从注册页面传递的用户名=" + uname +"密码=" + pwd );
        UserBean user = new UserBean();
        user.setUsername(uname);
        user.setPassword(pwd);

        model.addAttribute("user",user);

        return "forward:toLogin";

    }

    @RequestMapping("/toLogin")
    public String toLogin() {
        return "login.jsp";
    }

    @RequestMapping("/login")
    public ModelAndView doLogin(@RequestParam("u1") String uname, @RequestParam("p1") String pwd,
                                @SessionAttribute("user") UserBean userbean, ModelAndView view) {

        String msg ;
        if (uname.equals(userbean.getUsername()) && pwd.equals(userbean.getPassword())) {
            msg ="登录成功，欢迎进入系统..";
        } else {
            msg = "登录失败,用户名或密码不正确";
        }
//        model.addAttribute("msg",msg);
        view.addObject("msg",msg);
        view.setViewName("result.jsp");

//        return "result.jsp";
        return view;
    }
}
